__author__ = 'vivienfanghua'
import urllib2
from bs4 import BeautifulSoup
import codecs
import json
baseurl="http://api.map.baidu.com/place/v2/search?q=%E5%8C%BB%E7%96%97&region=%E6%AD%A6%E6%B1%89%E5%B8%82&page_size=20&page_num=pppnum&output=json&ak=ZWl4eD5D2leA0RxRw7c6hRwR"
output=codecs.open("D:/output.txt","w","utf8")
for i in range(20):
    url=baseurl.replace("pppnum",str(i))
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    content=response.read().decode("utf-8")
    js=json.loads(content)
    for j in js["results"]:
        name=j["name"]
        telephone=""
        if j.has_key("telephone"):
            telephone=j["telephone"]
        lat=j["location"]["lat"]
        lng=j["location"]["lng"]
        address=j["address"]
        output.write(name+u" "+unicode(lat)+u" "+unicode(lng)+u" "+address+u" "+telephone+u"\n")
print("Done")
